﻿using LigerUi.MVC.DataModel.EntityClasses.Models;
using Starring.Framework.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace LigerUi.MVC.DataAccess.CommonTools
{

    public static class CommonService
    {

        #region 单表的增删改查
        /// <summary>
        /// 分页，排序，过滤数据
        /// </summary>
        /// <typeparam name="TEntity">要返回的类型</typeparam>
        /// <param name="order">排序规则,如果没有，可以为null</param>
        /// <param name="paging">分页规则,必须有</param>
        /// <param name="where">过滤条件,如果没有，可以为null</param>
        /// <returns>List<TEntity></returns>
        public static List<TEntity> ReadList<TEntity>(DataOrder order, PagingEntity paging, Expression<Func<TEntity, bool>> where)
            where TEntity : class,new()
        {
            int intSkip = (paging.PageIndex - 1) * paging.PageSize;
            using (MVCEntities db = new MVCEntities())
            {
                IQueryable<TEntity> list = db.Set<TEntity>();

                if (!string.IsNullOrEmpty(order.OrderKey))
                {
                    list = list.OrderBy(order);
                }
                if (where != null)
                    list = list.Where(where);
                paging.RowCount = list.Count();
                list = list.Skip(intSkip).Take(paging.PageSize);

                return list.ToList();
            }
        }

        /// <summary>
        /// 单表操作
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Insert<TEntity>(TEntity entity) where TEntity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                bool result = false;
                db.Set<TEntity>().Add(entity);
                result = db.SaveChanges() > 0;
                return result;
            }
        }

        /// <summary>
        /// 一次保存多条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="List"></param>
        /// <returns></returns>
        public static bool Insert<TEntity>(List<TEntity> List) where TEntity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                bool result = false;
                foreach (TEntity entity in List)
                {
                    db.Set<TEntity>().Add(entity);
                }
                result = db.SaveChanges() > 0;
                return result;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Update<TEntity>(TEntity entity) where TEntity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                bool result = false;
                //db.Set<TEntity>().Attach(entity);
                db.Entry<TEntity>(entity).State = System.Data.EntityState.Modified;
                result = db.SaveChanges() > 0;
                return result;
            }
        }

        /// <summary>
        /// 一次性更新多条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="List"></param>
        /// <returns></returns>
        public static bool Update<TEntity>(List<TEntity> List) where TEntity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                bool result = false;
                foreach (TEntity entity in List)
                {
                    //db.Set<TEntity>().Attach(entity);
                    db.Entry<TEntity>(entity).State = System.Data.EntityState.Modified;
                }
                try
                {
                    result = db.SaveChanges() > 0;
                }
                catch
                {
                    //  result = false;
                }
                return result;
            }
        }

        /// <summary>
        /// 一次删除一条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Delete<TEntity>(TEntity entity) where TEntity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                bool result = false;
                //db.Set<TEntity>().Attach(entity);
                db.Entry<TEntity>(entity).State = System.Data.EntityState.Deleted;
                result = db.SaveChanges() > 0;
                return result;
            }
        }

        /// <summary>
        /// 一次性删除多条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="List"></param>
        /// <returns></returns>
        public static bool Delete<TEntity>(List<TEntity> list) where TEntity : class,new()
        {
            bool result = false;
            if (list.Count <= 0)
                return result;
            using (MVCEntities db = new MVCEntities())
            {
                foreach (TEntity entity in list)
                {
                    db.Entry<TEntity>(entity).State = System.Data.EntityState.Deleted;
                }
                result = db.SaveChanges() > 0;
                return result;
            }
        }

        /// <summary>
        /// 查找单条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="where">过滤条件，可以为null</param>
        /// <returns>返回数据的第一条</returns>
        public static TEntity Select<TEntity>(Expression<Func<TEntity, bool>> where) where TEntity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                if (where != null)
                {
                    return db.Set<TEntity>().FirstOrDefault(where);
                }
                else
                    return db.Set<TEntity>().FirstOrDefault();
            }
        }

        /// <summary>
        /// 查找多条数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="where">可以为null,如果为null那么将返回这个表里的所有数据</param>
        /// <returns></returns>
        public static List<Entity> SelectList<Entity>(Expression<Func<Entity, bool>> where) where Entity : class,new()
        {
            using (MVCEntities db = new MVCEntities())
            {
                if (where != null)
                {
                    return db.Set<Entity>().Where(where).ToList();
                }
                else
                    return db.Set<Entity>().ToList();
            }
        }
        #endregion

    }
}

